package com.deer.wms.ASN.manage.web;

import com.deer.wms.ASN.manage.model.SjcUpperTask;
import com.deer.wms.ASN.manage.model.SjcUpperTaskCriteria;
import com.deer.wms.ASN.manage.service.SjcUpperTaskService;
import com.deer.wms.base.system.model.Cell.CellInfo;
import com.deer.wms.base.system.service.CellInfoService;
import com.deer.wms.intercept.annotation.User;
import com.deer.wms.intercept.common.data.CurrentUser;
import com.deer.wms.inventory.model.Inventory.Inventory;
import com.deer.wms.inventory.model.Inventory.InventoryCriteria;
import com.deer.wms.inventory.service.InventoryService;
import com.deer.wms.project.root.core.result.CommonCode;
import com.deer.wms.project.root.exception.ServiceException;
import com.deer.wms.project.root.util.BeanUtils;
import com.deer.wms.project.root.util.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.deer.wms.project.root.core.result.Result;
import com.deer.wms.project.root.core.result.ResultGenerator;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
* Created by  on 2021/01/07.
*/
@Transactional
@RestController
@RequestMapping("/sjc/upper/tasks")
public class SjcUpperTaskController {

    @Autowired
    private SjcUpperTaskService sjcUpperTaskService;

    @Autowired
    private CellInfoService cellInfoService;

    @Autowired
    private InventoryService inventoryService;


    @PostMapping("/add")
    @Transactional
    public Result add(@RequestBody SjcUpperTask sjcUpperTask,@ApiIgnore @User CurrentUser currentUser) {
        if(currentUser == null){
            return ResultGenerator.genFailResult(CommonCode.MISSING_PERMISSION_PARAMTER, null);
        }
        Inventory inventory = inventoryService.getById(sjcUpperTask.getAcceptInventoryId());
        if(inventory == null){
            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"该库存码无效！");
        }
//        if(inventory.getQuantity()<sjcUpperTask.getQuantity()){
//            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"输入数量和实际数量不符，请校验数量！");
//        }
//        SjcUpperTask sjcUpperTask1 = sjcUpperTaskService.findBy("inventoryId",inventory.getInventoryId());
//        if(sjcUpperTask1 != null){
//            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"该库存已经到上架车！");
//        }
        if(inventory.getAllotQuantity()>0 || inventory.getBaoKuan()>0){
            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"该产品是爆款，请将"+inventory.getAllotQuantity()+"件产品送到打包台后，剩余的再上架！");
        }
        String fromCellCode = inventory.getCellCode();
        CellInfo fromCellInfo = cellInfoService.findByCodeAndWareId(fromCellCode,sjcUpperTask.getWareId());
        if(fromCellInfo==null){
            //原始货位有问题、
            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"未找到原始货位");
        }
        if(fromCellInfo.getCellUseType()!=0){
            //原始货位的货不在验收位上面
            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"该产品不在验收位置，不能上架");
        }

        String sjcCellCode = sjcUpperTask.getSjcCellCode();
        CellInfo cellInfo = cellInfoService.findByCodeAndWareId(sjcCellCode,sjcUpperTask.getWareId());
        if(cellInfo==null){
            //上架车货位号有问题
            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"未找到上架车货位");
        }
        if(cellInfo.getCellUseType()!=4){
            //此货位的使用类型不是上架车
            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"请扫描上架车货位");
        }
        InventoryCriteria inventoryCriteria = new InventoryCriteria();
        inventoryCriteria.setWareId(sjcUpperTask.getWareId());
        inventoryCriteria.setCellCode(sjcCellCode);
        List<Inventory> inventorys = inventoryService.findByCellCode(inventoryCriteria);
        for (Inventory inven:inventorys  ) {
            if(!inven.getItemCode().equals(inventory.getItemCode())){
                throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"该上架车位置已经有其他库存");
            }
        }
        sjcUpperTask.setCreateTime(DateUtils.getNowDateTimeString());
        sjcUpperTask.setCreateUserId(currentUser.getUserId());
        sjcUpperTask.setCreateUserName(currentUser.getUserName());

        sjcUpperTask.setItemCode(inventory.getItemCode());
        sjcUpperTask.setItemName(inventory.getItemName());
        sjcUpperTask.setSpec(inventory.getSpec());
        sjcUpperTask.setModel(inventory.getModel());
        sjcUpperTask.setImgUrl(inventory.getImgUrl());
        sjcUpperTask.setFromCellCode(inventory.getCellCode());
        //sjcUpperTask.setQuantity(inventory.getQuantity());
        sjcUpperTask.setWareId(inventory.getWareId());

        //如果之前入过相同的产品则放在相同货位或者相邻货位
        String itemCode = sjcUpperTask.getItemCode();
        String cellCode = sjcUpperTaskService.gerCellCode(itemCode,sjcUpperTask.getWareId(),sjcUpperTask.getOrgId());

        sjcUpperTask.setToCellCode(cellCode);


        //看一下这个上架车这个位置  是否有相同的sku
        SjcUpperTask oldTask = sjcUpperTaskService.find0ByCellCode(sjcUpperTask.getSjcCellCode(),sjcUpperTask.getWareId());

        Inventory newInventory = new Inventory();
        //把库存转移到上架车上面
        if(oldTask != null && oldTask.getInventoryId()!=null){
            newInventory = inventoryService.getById(oldTask.getInventoryId());
            newInventory.setQuantity(newInventory.getQuantity()+sjcUpperTask.getQuantity());
            inventoryService.updateById(newInventory);
            oldTask.setQuantity(sjcUpperTask.getQuantity()+oldTask.getQuantity());
            sjcUpperTaskService.updateById(oldTask);
        }else {
            BeanUtils.copyBeanProp(newInventory,inventory);
            newInventory.setInventoryId(null);
            newInventory.setCellCode(sjcUpperTask.getSjcCellCode());
            newInventory.setQuantity(sjcUpperTask.getQuantity());
            inventoryService.save(newInventory);

            sjcUpperTask.setInventoryId(newInventory.getInventoryId());
            sjcUpperTaskService.save(sjcUpperTask);
        }


        inventory.setQuantity(inventory.getQuantity()-sjcUpperTask.getQuantity());
        if(inventory.getQuantity()<0.1){
            inventoryService.removeById(inventory.getInventoryId());
        }else {
            inventoryService.updateById(inventory);
        }

        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/check")
    public Result check(  Integer sjcUpperTaskId,String toCellCode,Integer wareId,@ApiIgnore @User CurrentUser currentUser) {
        CellInfo cellInfo = cellInfoService.findByCodeAndWareId(toCellCode,wareId);
        if(cellInfo==null){
            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"货位无效！");
        }
        if(cellInfo.getCellUseType()!=2){
            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"目标位置必须是仓储位！");
        }
        SjcUpperTask sjcUpperTask = sjcUpperTaskService.getById(sjcUpperTaskId);
        if(sjcUpperTask.getState()==1){
            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"该任务已经完成！");
        }
        InventoryCriteria inventoryCriteria = new InventoryCriteria();
        inventoryCriteria.setCellCode(toCellCode);
        Integer skuLimit = 0;
        List<Inventory> list = inventoryService.findByCellCode(inventoryCriteria);
        if(list.size()>0){
            for (Inventory inventory:list
            ) {
                if(!inventory.getItemCode().equals(sjcUpperTask.getItemCode())){
                    skuLimit+=1;
                    if(cellInfo.getSkuLimit()==1){
                        throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"有其他上架车任务即将要放到该位置！");
                    }
                }
            }
        }

        SjcUpperTask sjcUpperTask1 = sjcUpperTaskService.find0ByToCellCode(toCellCode,wareId);
        if(sjcUpperTask1 !=null && !sjcUpperTask.getToCellCode().equals(toCellCode) && !sjcUpperTask1.getSjcUpperTaskId().equals(sjcUpperTaskId)){
            skuLimit+=1;
            if(cellInfo.getSkuLimit()==1){
                throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"有其他上架车任务即将要放到该位置！");
            }
        }
        if(cellInfo.getSkuLimit()<skuLimit){
            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"货位："+cellInfo.getCellCode()+" 已达到sku限制最大值-"+cellInfo.getSkuLimit()+"!");
        }
//        List<Inventory> list = inventoryService.findByCellCode(inventoryCriteria);
//        if(list.size()>0){
//            for (Inventory inventory:list
//            ) {
//                if(!inventory.getItemCode().equals(sjcUpperTask.getItemCode())){
//                    throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"该货位已经有其他货物！");
//                }
//            }
//        }
//
//        SjcUpperTask sjcUpperTask1 = sjcUpperTaskService.find0ByToCellCode(toCellCode,wareId);
//        if(sjcUpperTask1 !=null && !sjcUpperTask.getToCellCode().equals(toCellCode) && sjcUpperTask1.getSjcUpperTaskId() != sjcUpperTaskId){
//            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"有其他上架车任务即将要放到该位置！");
//        }


        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/commit")
    @Transactional
    public Result commit(  Integer sjcUpperTaskId,String toCellCode,Integer wareId,@ApiIgnore @User CurrentUser currentUser) {
//        CellInfo cellInfo = cellInfoService.findByCodeAndWareId(toCellCode,wareId);
//        if(cellInfo==null){
//            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"货位无效！");
//        }
//        if(cellInfo.getCellUseType()!=2){
//            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"目标位置必须是仓储位！");
//        }
//        SjcUpperTask sjcUpperTask = sjcUpperTaskService.getById(sjcUpperTaskId);
//        if(sjcUpperTask.getState()==1){
//            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"该任务已经完成！");
//        }
//        InventoryCriteria inventoryCriteria = new InventoryCriteria();
//        inventoryCriteria.setCellCode(toCellCode);
//        inventoryCriteria.setWareId(wareId);
//        Integer skuLimit = 0;
//        List<Inventory> list = inventoryService.findByCellCode(inventoryCriteria);
//        if(list.size()>0){
//            for (Inventory inventory:list
//            ) {
//                if(!inventory.getItemCode().equals(sjcUpperTask.getItemCode())){
//                    skuLimit+=1;
//                    if(cellInfo.getSkuLimit()==1){
//                        throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"有其他上架车任务即将要放到该位置！");
//                    }
//                }
//            }
//        }
//
//        SjcUpperTask sjcUpperTask1 = sjcUpperTaskService.find0ByToCellCode(toCellCode,wareId);
//        if(sjcUpperTask1 !=null && !sjcUpperTask.getToCellCode().equals(toCellCode) && !sjcUpperTask1.getSjcUpperTaskId().equals(sjcUpperTaskId)){
//            skuLimit+=1;
//            if(cellInfo.getSkuLimit()==1){
//                throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"有其他上架车任务即将要放到该位置！");
//            }
//        }
//        if(cellInfo.getSkuLimit()<skuLimit){
//            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"货位："+cellInfo.getCellCode()+" 已达到sku限制最大值："+cellInfo.getSkuLimit().intValue()+"!");
//        }
////        List<Inventory> list = inventoryService.findByCellCode(inventoryCriteria);
////        if(list.size()>0){
////            for (Inventory inventory:list
////                 ) {
////                if(!inventory.getItemCode().equals(sjcUpperTask.getItemCode())){
////                    throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"该货位已经有其他货物！");
////                }
////            }
////        }
////
////        SjcUpperTask sjcUpperTask1 = sjcUpperTaskService.find0ByToCellCode(toCellCode,wareId);
////        if(sjcUpperTask1 !=null && !sjcUpperTask.getToCellCode().equals(toCellCode) && sjcUpperTask1.getSjcUpperTaskId() != sjcUpperTaskId){
////            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"有其他上架车任务即将要放到该位置！");
////        }
//
//
//        Inventory inventory   =inventoryService.getById(sjcUpperTask.getInventoryId());
//        Inventory oldInventory = inventoryService.findByItemWareOrg(inventory.getItemCode(),inventory.getWareId(),inventory.getOrganizationId(),  toCellCode);
//        if(oldInventory != null){
//            oldInventory.setQuantity(oldInventory.getQuantity()+sjcUpperTask.getQuantity());
//            inventoryService.updateById(oldInventory);
//
//            //inventoryService.removeById(inventory.getInventoryId());
//        }else {
//            oldInventory = new Inventory();
//            BeanUtils.copyBeanProp(oldInventory,inventory);
//            oldInventory.setInventoryId(null);
//            oldInventory.setQuantity(sjcUpperTask.getQuantity());
//            oldInventory.setCellCode(toCellCode);
//            inventoryService.save(oldInventory);
//
//        }
//
//        inventory.setQuantity(inventory.getQuantity()-sjcUpperTask.getQuantity());
//        if(inventory.getQuantity()<0.1){
//            inventoryService.removeById(inventory.getInventoryId());
//        }else {
//            inventoryService.updateById(inventory);
//        }
//        sjcUpperTask.setState(1);
//        sjcUpperTask.setUpTime(DateUtils.getNowDateTimeString());
//        sjcUpperTask.setUpUserId(currentUser.getUserId());
//        sjcUpperTask.setUpUserName(currentUser.getUserName());
//        sjcUpperTask.setToCellCode2(toCellCode);
//        sjcUpperTaskService.updateById(sjcUpperTask);
        return ResultGenerator.genSuccessResult();
    }


    //直接上架
    @GetMapping("/zjUp")
    @Transactional
    public Result zjUp(Integer InventoryId,Double quantity,String toCellCode,Integer wareId,@ApiIgnore @User CurrentUser currentUser) {
//        CellInfo cellInfo = cellInfoService.findByCodeAndWareId(toCellCode,wareId);
//        if(cellInfo==null){
//            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"货位无效！");
//        }
//        if(cellInfo.getCellUseType()!=2){
//            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"目标位置必须是仓储位！");
//        }
//        Inventory inventory   =inventoryService.getById(InventoryId);
//        inventory.setItemCode(inventory.getItemCode());
//        if(inventory.getQuantity()<quantity){
//            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"请校验数量！");
//        }
//
//        Integer skuLimit = 0;
//        SjcUpperTask sjcUpperTask1 = sjcUpperTaskService.find0ByToCellCode(toCellCode,wareId);
//        if(sjcUpperTask1 !=null ){
//            skuLimit+=1;
//            if(cellInfo.getSkuLimit()==1){
//             throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"有其他上架车任务即将要放到该位置！");
//            }
//        }
//        InventoryCriteria inventoryCriteria = new InventoryCriteria();
//        inventoryCriteria.setCellCode(toCellCode);
//        List<Inventory> list = inventoryService.findByCellCode(inventoryCriteria);
//        if(list.size()>0){
//            for (Inventory inven:list
//            ) {
//                if(!inven.getItemCode().equals(inventory.getItemCode())){
//                    skuLimit+=1;
//                    if(cellInfo.getSkuLimit()==1){
//                        throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"有其他上架车任务即将要放到该位置！");
//                    }
//                }
//            }
//        }
//        if(cellInfo.getSkuLimit()<skuLimit){
//            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"货位："+cellInfo.getCellCode()+" 已达到sku限制最大值："+cellInfo.getSkuLimit().intValue()+"!");
//        }
//
////        SjcUpperTask sjcUpperTask1 = sjcUpperTaskService.find0ByToCellCode(toCellCode,wareId);
////        if(sjcUpperTask1 !=null ){
////            throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"有其他上架车任务即将要放到该位置！");
////        }
////        InventoryCriteria inventoryCriteria = new InventoryCriteria();
////        inventoryCriteria.setCellCode(toCellCode);
////        List<Inventory> list = inventoryService.findByCellCode(inventoryCriteria);
////        if(list.size()>0){
////            for (Inventory inven:list
////            ) {
////                if(!inven.getItemCode().equals(inventory.getItemCode())){
////                    throw  new ServiceException(CommonCode.SERVER_INERNAL_ERROR,"该货位已经有其他货物！");
////                }
////            }
////        }
//        Inventory oldInventory = inventoryService.findByItemWareOrg(inventory.getItemCode(),inventory.getWareId(),inventory.getOrganizationId(),  toCellCode);
//        if(oldInventory != null){
//            oldInventory.setQuantity(oldInventory.getQuantity()+quantity);
//            inventoryService.updateById(oldInventory);
//
//            //inventoryService.removeById(inventory.getInventoryId());
//        }else {
//            oldInventory = new Inventory();
//            BeanUtils.copyBeanProp(oldInventory,inventory);
//            oldInventory.setInventoryId(null);
//            oldInventory.setQuantity(quantity);
//            oldInventory.setCellCode(toCellCode);
//            inventoryService.save(oldInventory);
//
//        }
//
//        inventory.setQuantity(inventory.getQuantity()-quantity);
//        if(inventory.getQuantity()<0.1){
//            inventoryService.removeById(inventory.getInventoryId());
//        }else {
//            inventoryService.updateById(inventory);
//        }
//
        return ResultGenerator.genSuccessResult();
    }




    @GetMapping("/delete")
    public Result delete(  Integer id) {
        sjcUpperTaskService.removeById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(@RequestBody SjcUpperTask sjcUpperTask) {
        sjcUpperTaskService.updateById(sjcUpperTask);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/{id}")
    public Result detail(@PathVariable Integer id) {
        SjcUpperTask sjcUpperTask = sjcUpperTaskService.getById(id);
        return ResultGenerator.genSuccessResult(sjcUpperTask);
    }

    @GetMapping("/list")
    public Result list(SjcUpperTaskCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<SjcUpperTask> list = sjcUpperTaskService.findList(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @GetMapping("/findBySjcShellCode")
    public Result findBySjcShellCode( SjcUpperTaskCriteria criteria) {
        PageHelper.startPage(criteria.getPageNum(), criteria.getPageSize());
        List<SjcUpperTask> list = sjcUpperTaskService.findBySjcShellCode(criteria);
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(list);
    }

}
